import { HttpSeriveProvider } from "./../http-serive/http-serive";
import { Injectable } from "@angular/core";
import { MySeriveProvider } from "../my-serive/my-serive";
import { HttpClient, HttpHeaders } from "@angular/common/http";

/*
  Generated class for the PcSeriveProvider provider.

  See https://angular.io/guide/dependency-injection for more info on providers
  and Angular DI.
*/
@Injectable()
export class PcSeriveProvider {
  public headers = new HttpHeaders();
  token;
  public items = []; //保存创建项目时单项的数据
  imgdate = [
    {img : "http://netx-check.oss-cn-shenzhen.aliyuncs.com/photo/1532342289448.png"},
    {img : "http://netx-check.oss-cn-shenzhen.aliyuncs.com/photo/1532342325364.png"},
    {img : "http://netx-check.oss-cn-shenzhen.aliyuncs.com/photo/1532342344001.png"},
    {img : "http://netx-check.oss-cn-shenzhen.aliyuncs.com/photo/1532342588599.png"},
    {img : "http://netx-check.oss-cn-shenzhen.aliyuncs.com/photo/1532342629597.png"},
    {img : "http://netx-check.oss-cn-shenzhen.aliyuncs.com/photo/1532342650029.png"},
    {img : "http://netx-check.oss-cn-shenzhen.aliyuncs.com/photo/1532342665491.png"},
    {img : "http://netx-check.oss-cn-shenzhen.aliyuncs.com/photo/1532342692624.png"},
    {img : "http://netx-check.oss-cn-shenzhen.aliyuncs.com/photo/1532342738620.png"},
    {img : "http://netx-check.oss-cn-shenzhen.aliyuncs.com/photo/1532342770346.png"},
    {img : "http://netx-check.oss-cn-shenzhen.aliyuncs.com/photo/1532342781921.png"},
    {img : "http://netx-check.oss-cn-shenzhen.aliyuncs.com/photo/1532342793101.png"},
    {img : "http://netx-check.oss-cn-shenzhen.aliyuncs.com/photo/1532342806984.png"}
  ];
  constructor(
    public http: HttpClient,
    private httpSeriveProvider: HttpSeriveProvider,
    public mySeriveProvider: MySeriveProvider
  ) {
    //请求头的设置
    this.token = localStorage.getItem("token");

    this.headers = this.headers.set("Content-Type", "text/plain");
    this.headers = this.headers.set("Content-Type", "application/json");
    if (this.token) {
      this.headers = this.headers.set("token", this.token);
    } else if (this.token === null || this.token === undefined) {
      this.headers = this.headers.set("token", "");
    } else {
      this.headers = this.headers.set("token", null);
    }
  }

  setToken(){
    this.headers = this.headers.set('token', localStorage.getItem("token"));
  }
  
  //随机数
  rand(){ 
    let rand = Math.floor(Math.random()*this.imgdate.length);
    return rand;
  }

  /**
   * 创建评测
   * @param data JSON对象
   */
  createPc(data) {
    const stringevaluating = JSON.stringify(data)
      .replace(/":{/g, '":\\"{')
      .replace(/}}/g, '}\\"}')
      .replace(/{\\"diff\\"/g, "{'diff'")
      .replace(/,\\"sort\\"/g, ",'sort'");
    return this.httpSeriveProvider.post(
      `${this.httpSeriveProvider.api}evaluate/create`,
      stringevaluating,
      { headers: this.headers }
    );
  }

  /**
   * 获取评测大项
   */
  getAllEvalList() {
    return this.httpSeriveProvider.get(
      `${this.httpSeriveProvider.api}evaluate/all?param=${new Date()}`,
      { headers: this.headers }
    );
  }

  /**
   * 获取评测小项
   * @param evaluatingCode 评测Code
   */
  getSmEvalList(evaluatingCode) {
    return this.httpSeriveProvider.get(
      `${this.httpSeriveProvider.api}evaluate/user/${evaluatingCode}`,
      { headers: this.headers }
    );
  }

  /**
   * 发送对他人评测内容
   * @param evalCon 评测内容，JSON对象
   */

  postEval(evalCon) {
    const stringEvaluatingDescList = JSON.stringify(evalCon)
      .replace(/:{"name":"/g, ":\"[{'name':'")
      .replace(/","value"/g, "','value'")
      .replace(/},"usercode"/g, '}]","usercode"');

    return this.httpSeriveProvider.post(
      `${this.httpSeriveProvider.api}evaluate/desc/create`,
      stringEvaluatingDescList,
      { headers: this.headers }
    );
  }

  /**
   * 获取他团队的评测报告和最新一次评测的优秀成员接口
   * @param teamCode 团队Code
   */

  getTeam(teamCode) {
    const evaluating = teamCode;
    this.headers = this.headers.set("Content-Type", "application/json");
    return this.httpSeriveProvider.post(
      `${this.httpSeriveProvider.api}evaluate/team`,
      evaluating,
      { headers: this.headers }
    );
  }

  /**
   * 根据评测排序规则排序接口
   * @param evaluatingCode 评测ID
   * @param Type 评测小项名称
   */

  getTeamRp(data) {
    const stringSortEvaluate = JSON.stringify(data);
    return this.httpSeriveProvider.post( 
      `${this.httpSeriveProvider.api}evaluate/sort`,
      stringSortEvaluate,
      { headers: this.headers }
    ); 
  }

  /**
   * 获取自己评测的平均值
   * @param evalCode 评测ID
   * @param userCode 用户ID
   */

  getMyRp(data) {
    const stringfindEvaluatingDesc = JSON.stringify(data);
    return this.httpSeriveProvider.post(
      `${this.httpSeriveProvider.api}evaluate/desc/own`,
      stringfindEvaluatingDesc,
      { headers: this.headers }
    );
  }

   /**
   * 是否有权限创建评测 
   */
  getCanCreateEvaluating(teamCode) {
    let h = this.headers;
    h.delete("Content-Type");
    h = h.set('Content-Type', 'application/json');
    return this.httpSeriveProvider.post(
      `${this.httpSeriveProvider.api}evaluate/check`,
      { teamcode: teamCode },
      { headers: h }
    );
  }

  /**
   * 获取成员个人信息
   * @param userCode 用户ID
   */
  getuser(userCode) {
    return this.httpSeriveProvider.get(
      `${this.httpSeriveProvider.api}user/info/userCode/${userCode}`,
      { headers: this.headers }
    );
  }

  /**
   * 检测当前项目时候评价完成
   * @param evaluatingCode 评测ID
   */
  getevalval(evaluatingCode) {
    return this.httpSeriveProvider.get(
      `${this.httpSeriveProvider.api}evaluate/check/${evaluatingCode}`,
      { headers: this.headers }
    );
  }

  /**
   * 获取自己评测过的内容
   * @param type 评测单项的id 0是第一项
   * @param evaluatingCode 评测ID
   */
  getMyValue(data) {
    const sortEvaluate = data;
    this.headers = this.headers.set("Content-Type", "application/json");
    return this.httpSeriveProvider.post(
      `${this.httpSeriveProvider.api}evaluate/user/score`,
      sortEvaluate,
      { headers: this.headers }
    );
  }
}
